<template>
    <!-- 页面头部 -->
    <HomePageHead></HomePageHead>
    <!-- 导航栏 -->
    <HomePageNavigation></HomePageNavigation>
    <!-- 列表页广告一 -->
    <HomeTopTen :imgurl="adImg1" v-if="adImg1"></HomeTopTen>
    <!-- 资讯列表 -->
    <div class="newsDetail">
        <div class="inner">
            <div class="innerLeft">
                <section class="book_son_right clearfix">
                    <div class="book_son_right_ul clearfix">
                        <div class="book_son_right_li clearfix" v-for="(item, index) in newsDetail" :key="index">
                            <div class="book_son_right_li_img">
                                <img class="book_son_right_li_img" :src="item.img_url" :title="item.title"
                                    v-if="item.img_url">
                            </div>
                            <div class="book_son_right_li_in clearfix">
                                <h4 class="book_son_right_li_h4 dot1">{{ item.title }}</h4>
                                <div class="book_son_right_li_dot3 dot3">
                                    {{ item.description }}
                                </div>
                                <NuxtLink :href="`/${item.pinyin}/${item.id}.html`" class="book_son_right_li_btn"
                                    :title="item.name">
                                    查看详情
                                </NuxtLink>
                            </div>
                        </div>
                    </div>
                </section>
                <!-- 分页器 -->
                <div class="pagination" v-if="total > 0">
                    <el-pagination size="small" background layout="prev, pager, next" :total="total" class="mt-4"
                        :page-size="pageSize" prev-text="上一页" next-text="下一页" @change="changePage" />
                </div>
            </div>
            <div class="innerRight">
                <div class="rightMenuTitle">
                    <NuxtLink :to="`/${parent_pinyin}/index.html`"> {{ parent_name }}</NuxtLink>
                </div>
                <ul>
                    <li v-for="(item, index) in bottomMenu" :key="index">
                        <NuxtLink :to="`/${item.pinyin}/list-1.html`" :title="item.name"
                            v-if="item.category_id == pageId" class="active">
                            {{ item.alias }}
                        </NuxtLink>
                        <NuxtLink :to="`/${item.pinyin}/list-1.html`" :title="item.name"
                            v-else-if="item.category_id != pageId">
                            {{ item.alias }}
                        </NuxtLink>
                    </li>
                </ul>
            </div>
            <div style="clear: both;"></div>
        </div>
    </div>
    <!-- 广告二 -->
    <HomeTopTen :imgurl="adImg2" v-if="adImg2"></HomeTopTen>
    <!-- 页面底部 -->
    <HomeFoot1></HomeFoot1>
</template>

<script setup>
//1.页面依赖 start ---------------------------------------->
import { ElPagination } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue';

//获得跳转过来的id
const route = useRoute();
//获得当前的完整路径
const fullPath = route.path;
//拆分，取出来中间这一段，然后提取数字部分
const segments = fullPath.split('/');
const targetSegment = segments[2];
const targetSegment1 = segments[1];

//const numberPart = targetSegment.match(/\d+$/)?.[0]; 
let articleId;
let pageId;
let routeId
let activeId = parseInt(route.params.id);
console.log('activeId', activeId);

//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment1+'/'+targetSegment,
    },
});

if (getRouteId.code == 200) {
    articleId = getRouteId.data.id;
    pageId = getRouteId.data.category_id;
} else {
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    console.log("错误位置：通过url路径查询导航池id")
    console.log("后端错误反馈：", getRouteId.message)
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
}

//通过导航路径反向查询导航id
const getRouteId1 = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment1,
    },
});

if (getRouteId1.code == 200) {
    routeId = getRouteId1.data.category_id;
} else {
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    console.log("错误位置：通过url路径查询导航池id")
    console.log("后端错误反馈：", getRouteId1.message)
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
}

//面包屑导航
const parent_name = ref("");
const parent_id = ref("");
const parent_pinyin = ref("");
let getParentNav = async () => {
    const listData = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': routeId,
        },
    });

    if (listData.code == 200) {
        parent_name.value = listData.data.alias;
        parent_id.value = listData.data.parent_id;
        parent_pinyin.value = listData.data.aLIas_pinyin;

    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取面包屑导航")
        console.log("后端错误反馈：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//获得父级栏目详情
getParentNav();

//1.页面依赖 end ---------------------------------------->

//2.页面数据 start ---------------------------------------->
//广告列表
const adList = ref([]);
let adImg1 = ref([]);
let adImg2 = ref([]);
async function getAdData() {
    const adData = await requestDataPromise('/web/getWebsiteAdvertisement', { method: 'GET', query: { 'ad_tag': 'PAGE' } });
    adList.value = adData.data;
    if (adData.code == 200) {
        for (let item of adData.data) {
            if (item.ad_tag == 'PAGE_0001') {
                adImg1.value = item;
            }
            if (item.ad_tag == 'PAGE_0002') {
                adImg2.value = item;
            }
        }
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取详情页广告列表")
        console.log("后端错误反馈：", adData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
getAdData();

const newsDetail = ref({})
const bottomMenu = ref([]);
const total = ref(0);//总条数
const page = ref(1);//当前页码
const pageSize = ref(10); //每页显示的条数

async function getPageMenu() {
    const mkdata = await requestDataPromise('/web/getWebsiteBookList', {
        method: 'GET',
        query: {
            'id': pageId,
            'page': page.value,
            'pageSize': pageSize.value,
        },
    });
    bottomMenu.value = mkdata.data.category;
    newsDetail.value = mkdata.data.books;
    total.value = mkdata.data.count;
}
getPageMenu();
//2.页面数据 end ---------------------------------------->

//3.分页器 start ---------------------------------------->
//分页事件
let changePage = (value) => {
    console.log("当前页码", value);
    page.value = value;
    getPageMenu()
}
//3.分页器 end ---------------------------------------->

//4.设置seo信息 start---------------------------------------->
//4.1 设置seo信息
const setData = await requestDataPromise('/web/getWebsiteFootInfo', {
    method: 'GET',
    query: {},
});

let seoTitle = setData.data.website_head.title;
let seoDescription = setData.data.website_head.description;
let seoKeywords = setData.data.website_head.keywords;
let seoSuffix = setData.data.website_head.suffix;
let seoName = setData.data.website_head.website_name;

useSeoMeta({
    title: seoTitle + "_" + seoSuffix,
    meta: [
        { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix, tagPriority: 10 },
        { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix, tagPriority: 10 },
        { name: 'viewport', content: 'width=device-width,initial-scale=1,user-scalable=no',tagPriority: 10 }
    ]
});
//4.设置seo信息 end---------------------------------------->

onMounted(async () => {
    //从客户端获取行政职能部门 加快打开速度
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snzxw_page_0001`
    const responseAd1 = await fetch(url, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd1 = await responseAd1.json();
    adImg1.value = resultAd1.data[0];

    //广告2
    let url2 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snzxw_page_0002`
    const responseAd2 = await fetch(url2, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd2 = await responseAd2.json();
    adImg2.value = resultAd2.data[0];
})
</script>

<style lang="less" scoped>
@import '@/assets/css/shukanList.less';
</style>
